/*
 * @Author: hq
 * @Date: 2022-06-09 09:47:30
 * @LastEditors: hq
 * @LastEditTime: 2022-06-09 18:40:42
 * @Description: your project
 * @version: 1.0
 */
//key:   value:
export default function myCache(key, value, seconds = 3600 * 6) { //默认6小时
    let nowTime = Date.parse(new Date()) / 1000;
    (seconds, 'seconds')
    if (key && value) {
        let expire = nowTime + Number(seconds);
        sessionStorage.setItem(key, JSON.stringify(value) + '|' + expire)
        ('已经把' + key + '存入缓存,过期时间为' + expire)
    } else if (key && !value) {
        let val = sessionStorage.getItem(key);
        if (val) {
            // 缓存存在，判断是否过期
            let temp = val.split('|')
            if (!temp[1] || temp[1] <= nowTime) {
                sessionStorage.removeItem(key)
                (key + '缓存已失效')
                return '';
            } else {
                return JSON.parse(temp[0]);
            }
        }
    }

}